package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.dto.request.UmsAdminLoginParam;
import com.yc.cloud.admin.dto.request.UmsAdminCreateParam;
import com.yc.cloud.admin.service.UmsAdminService;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.entity.UmsAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "auth")
@Api(value = "权限认证", tags = "权限认证")
public class AuthController {

    @Resource
    private UmsAdminService adminService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @ApiOperation(value = "登录", notes = "登录成功后返回Token,最终客户端需在Header中增加Authorization参数名,值为登录返回值")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<String> login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        String token = adminService.queryByUserNameAndPassword(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (token == null) {
            return ResponseResult.failed("用户名或密码错误");
        }
        val result = tokenHead + token;
        return ResponseResult.success(result);
    }

    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<UmsAdmin> register(@Validated @RequestBody UmsAdminCreateParam umsAdminParam) {
        var umsAdmin = adminService.queryByUserName(umsAdminParam.getUsername());
        if (umsAdmin != null) {
            return ResponseResult.failed("用户名已存在");
        }
        umsAdmin = adminService.register(umsAdminParam);
        if (umsAdmin == null) {
            return ResponseResult.failed();
        }

        return ResponseResult.success(umsAdmin);
    }


//    @ApiOperation(value = "微信分享")
//    @PostMapping(value = "/wx/share")
//    public ResponseResult<Map<String, String>> getShareInfo(@RequestBody WxParam param) {
//        var accessToken = umsAdminCacheService.getWXAccessToken();
//        if (StrUtil.isEmpty(accessToken)) {
//            accessToken = OfficialAccountUtils.getAccessToken();
//            umsAdminCacheService.setWXAccessToken(accessToken);
//        }
//        if (StrUtil.isEmpty(accessToken)) {
//            return ResponseResult.failed("获取AccessToken失败");
//        }
//        var ticket = umsAdminCacheService.getWXTicket();
//        if (StrUtil.isEmpty(ticket)) {
//            ticket = OfficialAccountUtils.getTicket(accessToken);
//            umsAdminCacheService.setWXTicket(ticket);
//        }
//        if (StrUtil.isEmpty(ticket)) {
//            return ResponseResult.failed("获取Ticket失败");
//        }
//        val result = OfficialAccountUtils.sign(ticket, param.getUrl());
//        return ResponseResult.success(result);
//    }
}
